Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfcs: graph: support int4/int8 compression for K/V in fused SDPA #2041

Open
wants to merge 1 commit into
base: rfcs
Choose a base branch
from

Conversation

wzt1997
Copy link
Contributor

@wzt1997 wzt1997 commented Aug 16, 2024

Description

This is to propose to support int4/int8 compression for K/V in fused SDPA.
Link to the rendered document.

@wzt1997 wzt1997 added the RFC A design document label Aug 16, 2024
@wzt1997 wzt1997 self-assigned this Aug 16, 2024
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from 943a6b3 to 83217a2 Compare August 16, 2024 06:57
@vpirogov vpirogov changed the title rfcs: graph: support int4/int8 compression for K/V in fuesd SDPA rfcs: graph: support int4/int8 compression for K/V in fused SDPA Aug 16, 2024
inputs should be `1d` tensors.
2. For `per_group` quantization, all dimensions should match the input,
excepts for the dimension where grouped quantization applies, which should
be `src_dim / group_size`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how that matches the case when Batch dimension should be broadcasted for scales/zero-points:

W = [B, K, N]
pre-scale W: [1, gK, N] x [B, K, N] = W'
matmul: [B, M, K] x W' = [B, M, N]

Use case I've seen are like that, batch dimension doesn't receive its own dimension of scales.

Copy link
Contributor Author

@wzt1997 wzt1997 Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing the case. According to potential request from IPEX and per-token quantization, we decide to update the scale/zp input shape requirement for per-group quantization for scalability and flexibility. One potential option is to allow 1 on dimensions other than the last two, K and N.

rfcs/20240808-graph-api-int-compression-for-sdpa/README.md Outdated Show resolved Hide resolved
rfcs/20240808-graph-api-int-compression-for-sdpa/README.md Outdated Show resolved Hide resolved
rfcs/20240808-graph-api-int-compression-for-sdpa/README.md Outdated Show resolved Hide resolved
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from 1d5e1d2 to d633cf7 Compare August 27, 2024 08:31
@wzt1997 wzt1997 requested a review from a team as a code owner August 27, 2024 08:31
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch 4 times, most recently from 04a94db to 5b84ad0 Compare August 29, 2024 00:28
/// 4-bit signed integer.
dnnl_s4 = 11,
/// 4-bit unsigned integer.
dnnl_u4 = 12,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For API completeness, I feel yes we need to add both s4 and u4. Just for my curiosity, do you know if both s4 and u4 are used for this int4 K/V compression request? If both are used, any difference in quantization recipe between s4 and u4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to user request, they are likely to use s4 for KV storage. But as the KV compression is still WIP, it might change in the future. Regarding the difference between u4 and s4 recipes, since int4 data types always use asymmetric quantization, the parameters will be quite similar for u4 and s4. The difference should be mainly related to the de-quantization logic.

@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from 5b84ad0 to 6f68c51 Compare September 2, 2024 02:49
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from 6f68c51 to f1c5003 Compare September 6, 2024 09:06
@vpirogov vpirogov added this to the v3.7 milestone Sep 9, 2024
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch 2 times, most recently from c6ed1d1 to f1c0722 Compare September 23, 2024 02:43
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from f1c0722 to a8a3a20 Compare October 8, 2024 07:53
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch 2 times, most recently from 4aae2bf to 0ac893e Compare October 30, 2024 07:51
@wzt1997 wzt1997 force-pushed the zhitao/rfc/graph-int4-support branch from 0ac893e to 810ef3e Compare November 1, 2024 06:03
Copy link
Contributor

@dzarukin dzarukin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the document. I left some minor comments.

Comment on lines +140 to +141
are required for each hidden dimension to maintain the model accuracy. The
requirement for grouped zero points is not promised. See [int4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirement for grouped zero points is not promised.

Could you, please, elaborate on the meaning of the phrase and how it affects the proposal.


1. Add `per_group` to the supported values of `qtype` attribute, and the default
value will be unchanged.
2. The existing attribute `axis` will ignored if `per_group` quantization is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. The existing attribute `axis` will ignored if `per_group` quantization is
2. The existing attribute `axis` will be ignored if `per_group` quantization is

each quantization group( which is `group_size` ). And the other dimensions
should be all `1`. The attribute is required when `per_group` quantization
type is specified for `qtype`. If `per_group` quantization is not specified
and `group_shape` attribute is given, it will be ignored.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it's implicitly assumed that the number of dimensions in the group_shape attribute coincides with the number of dimensions of the input shape. It would good to explicitly word it out if that's the case.

const size_t nG = 2, G = N / nG;

dims src_dims = {K, N};
dims scale_dims = {K, N/G};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dims scale_dims = {K, N/G};
dims scale_dims = {K, nG};

// Specify the quantization type as per_group quantization.
deq.set_attr<std::string>(op::attr::qtype, "per_group");
// Axis indicates on which dimension the quantization will be applied.
deq.set_attr<int64_t>(op::attr::axis, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is not needed any longer.

// ...
```

##### Limitation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### Limitation
#### Limitation

Comment on lines +243 to +246
Similar to the discussion of int8 and fp8 data types, an alternative solution
may be supporting int4 data types directly in computation operations like MatMul
and Convolution. But it will bloat and complicate the opset and op schema of
oneDNN Graph. Hence it's not considered here.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this paragraph is not quite relevant as the path for integer data types was already paved and it won't change.

I'd rather replace it with why a vector over a single value + axis combination as it was initially. This is more valuable information for future reference.

Comment on lines +248 to +253
In addition, to enhance clarity and reduce compilation overhead, oneDNN Graph
will support the direct quantization between int4 data types and bf16/f16.
This means that additional `Typecast` operations will no longer be necessary
between quantization processes and subsequent operations in bf16/f16 computation
graphs. Although it will complicate the fusion patterns to some extend, we can
address this by implementing more precise pattern definitions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this paragraph belongs to a Proposal 3 section instead since it already has TypeCast dropped from the picture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC A design document
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants